﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Epic.HandyExtensions
{
    public static class EnumerableExtensions
    {
#pragma warning disable 1591

        public static IEnumerable<T> AntiNull<T>(this IEnumerable<T> source)
        {
            return source ?? new T[] { };
        }

        public static IEnumerable<T> ForEach<T>(this IEnumerable<T> source, Action<T> action)
        {
            if (action != null)
            {
                foreach (var item in source)
                    action(item);
            }
            return source;
        }

        public static IEnumerable<T> ForEachThen<T>(this IEnumerable<T> source, Action<T> action)
        {
            if (action == null)
                return source;

            return source.Select(item => {
                action(item);
                return item;
            });
        }

        public static IEnumerable<T> ToFakeEnumerable<T>(this IEnumerator<T> enumerator)
        {
            //while (enumerator.MoveNext())
            //    yield return enumerator.Current;

            return new FakeEnumerable<T>(enumerator);
        }
    }
}
